<?

class XML_RPC
{
	var $client;
	var $host;
	var $port;
	var $protocol;
	var $http_protocol;
	var $debug;

	function XML_RPC( $params = array())
	{
		$this->host = isset($params['host'])?$params['host']:'127.0.0.1';
		$this->port = isset($params['port'])?$params['port']:'80';
		$this->protocol = isset($params['protocol'])?$params['protocol']:'http';
		// http
		$this->http_protocol = isset($params['http_protocol'])?$params['http_protocol']:'1.1';
	}

	function connect()
	{

		switch($this->protocol)
		{

			case 'http':
			        $this->client = new Net_HTTP_Client( $this->host, $this->port );
			        $this->client->setProtocolVersion( $this->http_protocol );
				break;

			default:
				$this->_error = 'protocolo desconocido '.$this->protocol;
				return 0;
				break;
		}
	}

	function call($method)
	{
		$numargs = func_num_args();
		$args = func_get_args(); 

		for ($i = 1; $i < $numargs; $i++)
		{
		    if(is_array($args[$i]))
	 	    {
			$ary =& $args[$i];
			$count = count($ary);
			for($j = 0; $j < $count; $j++)
			{
				$params[] = $ary[$j];
			}
		    }
		    else
		    {
			$params[] = $args[$i];
		    }
		}
	        $request = xmlrpc_encode_request($method, $params);

		if($this->debug) print "<pre>request\n".htmlentities($request)."</pre>";

		switch($this->protocol)
		{

			case 'http':
	        		if( $this->client->Post( "/RPC2", $request) == 200 )
	        			$response = $this->client->getBody();
				break;
	
			default:
				break;
		}

		if($this->debug) print "<pre>response\n".htmlentities($response)."</pre>";

        	$result = xmlrpc_decode ($response);
		if(@array_key_exists('faultCode', $result))
		{
			$this->faultCode = $result['faultCode'];
			$this->faultString = $result['faultString'];
			$this->debase64($this->faultString);
			return FALSE;
		}
		$this->faultCode = $result[0];
		$this->faultString = $result[1];
		$this->debase64($this->faultString);
		$this->debase64($result[2]);
		return $result[2];
	}

	function debase64(&$var)
	{
		if(is_object($var))
		{
			if($var->xmlrpc_type == 'base64')
			{
				$var = $var->scalar;
			}
		}
		if(is_array($var))
		{
			$keys = array_keys($var);
			for($i=0;$i<count($keys);$i++)
			{
				$this->debase64($var[$keys[$i]]);
			}
		}
	}

	function listmethods()
	{
	        $request = xmlrpc_encode_request('system.listMethods',array());
		switch($this->protocol)
		{

			case 'http':
	        		if( $this->client->Post( "/RPC2", $request) == 200 )
	        			$response = $this->client->getBody();
				break;
	
			default:
				break;
		}

        	$result = xmlrpc_decode ($response);

		if(@array_key_exists('faultCode', $result))
		{
			$this->faultCode = $result['faultCode'];
			$this->faultString = $result['faultString'];
			return FALSE;
		}

		$this->debase64($result);
		return $result;
	}

	function perror($error = '')
	{
		if($error == '')
			$error = $this->faultCode;

		$errstr = $this->faultString;

		switch($error)
		{
			case 'ERROR':
				$errstr = 'Ha ocurrido un error';
				break;

			case 'EVHSTS':
				$errstr = 'La zona aun tiene sitios';
				break;

			case 'EFTPS':
				$errstr = 'La zona aun tiene usuarios FTP';
				break;
		}

		return $errstr;
	}

	function disconnect()
	{
		switch($this->protocol)
		{

			case 'http':
				$this->client->disconnect();
				break;

			default:
				break;
		}

	}

}
?>
